<template>
	<view class="">
		<image class="bg" src="/static/user/loginBg.png"></image>
		<view class="box">
			<view class="title">注册</view>
			<view v-if="type == 1">
				<view class="phone flex">
					<text class="mr-20">+86</text>
					<input placeholder="输入手机号码" type="number" maxlength="11" v-model="phone" />
				</view>
				<view class="phone flex flexSbetween">
					<input style="width: 300rpx;" placeholder="输入验证码" v-model="authCode" />
					<text class="ml-20 authCode" @click="code()">{{btnMsg}}</text>
				</view>
				<button @click="next">下一步</button>
				<!-- <view class="flex flexJusCenter mt-40 font28" @click="login">账号登陆</view> -->
			</view>
			<view v-if="type == 2">
				<view class="font28 mt-20">设置你的用户名</view>
				<view class="phones">
					<input placeholder="输入用户名" v-model="name" />
				</view>
				<view class="font28 mt-40">设置密码</view>
				<view class="phones flex flexSbetween">
					<input placeholder="输入密码" :password="passStatus" v-model="pass" />
					<u-icon name="eye-off" color="#444" size="40rpx" @click="checkPass" v-if="!passStatus"></u-icon>
					<u-icon name="eye" color="#444" size="40rpx" @click="checkPass" v-if="passStatus"></u-icon>
				</view>
				<view class="font28 mt-40">确认密码</view>
				<view class="phones flex flexSbetween">
					<input placeholder="确认密码" password v-model="confirmPass" />
				</view>
				<button @click="register">注册完成</button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				btnMsg: '发送验证码',
				time: '',
				phone: '',
				authCode: '',
				type: 1,
				name: '',
				passStatus: true,
				pass: '',
				confirmPass: ''
			}
		},
		onLoad() {

		},
		methods: {
			next() {
				if (!(/^1\d{10}$/).test(this.phone)) {
					uni.showToast({
						title: '请输入正确的手机号',
						icon: 'none'
					})
					return
				}
				if(!this.authCode){
					uni.showToast({
						title: '请输入验证码',
						icon: 'none'
					})
					return
				}
				this.type = 2
			},
			code() {
				if (!(/^1\d{10}$/).test(this.phone)) {
					uni.showToast({
						title: '请输入正确的手机号',
						icon: 'none'
					})
					return
				}
				let data={
					mobile:this.phone,
					event:'register'
				}
				this.api.post('api/sms/send',data).then((res)=>{
					if(res.code == 200){
						uni.showToast({
							title:res.msg,
							icon:'none'
						})
						this.time = 60;
						let t = setInterval(() => {
							if (this.time > 0) {
								this.btnMsg = this.time + "s后获取";
								this.time--;
							} else {
								this.btnMsg = "获取验证码";
								this.time = 60;
								clearInterval(t);
							}
						}, 1000);
					}else{
						uni.showToast({
							title:res.msg,
							icon:'none'
						})
					}
				})
			},
			checkPass() {
				this.passStatus = !this.passStatus
			},
			register() {
				if(!this.name){
					uni.showToast({
						title: '请输入用户名',
						icon: 'none'
					})
					return
				}
				if(!this.pass){
					uni.showToast({
						title: '请输入密码',
						icon: 'none'
					})
					return
				}
				if(this.confirmPass != this.pass){
					uni.showToast({
						title: '两次密码不一致',
						icon: 'none'
					})
					return
				}
				let data={
					username:this.name,
					mobile:this.phone,
					password:this.pass,
				}
				this.api.post('api/user/register',data).then((res)=>{
					if(res.code == 200){
						uni.showToast({
							title:res.msg,
							icon:'none'
						})
						setTimeout(()=>{
							uni.navigateBack({
								delta:1
							})
						},1000)
					}else{
						uni.showToast({
							title:res.msg,
							icon:'none'
						})
					}
				})
			}
		}
	}
</script>

<style>
	.bg {
		width: 100%;
		height: 100%;
		position: fixed;
		left: 0;
		right: 0;
		top: 0;
		bottom: 0;
	}

	.box {
		background: rgba(255, 255, 255, 0.8);
		position: absolute;
		left: 0;
		right: 0;
		margin: 50% 40rpx;
		border-radius: 40rpx;
		padding: 40rpx;
	}

	.title {
		font-size: 50rpx;
		font-weight: 700;
		color: #000;
		padding-bottom: 60rpx;
	}

	.phone {
		background: rgba(255, 255, 255, 0.9);
		border-radius: 50rpx;
		padding: 25rpx 40rpx;
		margin: 40rpx 0;
	}

	.phones {
		background: rgba(255, 255, 255, 0.9);
		border-radius: 50rpx;
		padding: 25rpx 40rpx;
		margin: 20rpx 0;
	}

	.authCode {
		font-size: 28rpx;
		color: #353535;
	}

	button {
		background: #F87985;
		color: #fff;
		border-radius: 50rpx;
		margin-top: 80rpx;
	}
</style>